package midalgorithms.backtrack;

import java.util.ArrayList;
import java.util.List;

public class _03Permute {
    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> permuteNums = new ArrayList<>(nums.length);
        int remainNum = nums.length;
        backtrack(res,permuteNums,nums,remainNum);
        return res;
    }

    //  一次里程碑，我由衷的感到高兴！
    private static void backtrack(List<List<Integer>> res, List<Integer> permuteNums, int[] nums, int remainNum) {
        if (remainNum == 0){
            List<Integer> list = new ArrayList<>(permuteNums);
            res.add(list);
            return;
        }
        //  回溯
        for (int num : nums) {
            //  如果permuteNums中不包含当前元素，则添加
            if (!permuteNums.contains(num)) {
                permuteNums.add(num);
                backtrack(res, permuteNums, nums, remainNum - 1);
                permuteNums.remove(permuteNums.size()-1);
            }
        }
    }
}
